<?php

/** 
 * $Source: /cvsroot/pn-commerce/pn-commerce/pntemplates/plugins/function.pncReadEZComments.php,v $
 * $Author: landseer $
 * $Revision: 1.3 $
 * $Date: 2004/08/09 13:45:42 $
 *
 * Type: Function
 *
 * Read the comments written by our customers
 * If EZComments is not installed, this plugins silently returns
 *@param params['numComments'] int number of comments to read, default = 3
 *@param params['ItemID'] int the id of the item, if not set, we will the overall last numComments entries
 *@return string processed smarty output
 */
function smarty_function_pncReadEZComments($params, &$smarty)
{
    extract($params);
    unset($args);
    
    require_once('modules/pncommerce/common.php');

    if( !pnModAPILoad( 'pncommerce', 'user' ) ) {
        $smarty->trigger_error( 'pncReadEZComments: cannot load pncommerce userapi', E_ERROR ); 
        return false;
    }

    if( !pnModLoad( 'EZComments', 'user' ) ) {
        // silently return
        return false;
    }

    $numComments = (isset($numComments)) ? $numComments : 3;
    
    list($dbconn, $tablename, $tablecolumn ) = pncOpenDB('EZComments');

    $sql = "SELECT $tablecolumn[objectid]
                   $tablecolumn[comment],
                   $tablecolumn[uid]
            FROM $tablename
	        WHERE $tablecolumn[modname]='pncommerce' ";
    $result = $dbconn->Execute($sql);
    // check for errors or no rows found, and if so return false
    if (($dbconn->ErrorNo() != 0) || ($result->EOF) ) { return false; }

    if(isset($ItemID)) {
        // item id specified, read the latest x comments
	    $sql .= "AND $column[objectid]='".(int)$ItemID."' ";
    }
    $sql .= "ORDER BY DATE DESC LIMIT ".(int)$numComments;
    
    $smarty = new pnRender( 'pncommerce' );
    $comarray = array();
    if( $result->RecordCount > 0 ) {
        for (; !$result->EOF; $result->MoveNext()) {
            list($itemid, $com['Text'], $uid) = $result->fields;
            $com['Item'] = pnModAPIFunc( 'pncommerce', 'user', 'getItem', array( 'ItemID' => $itemid ) );
            $com['UName'] = pnUserGetVar( 'uname', $uid );
            array_push( $comarray, $com );
        }
    	$smarty->assign( 'Comments', $comarray );
	
    }
    return false;

}
?>
